<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_page_w_grid_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/grids/_ci/ci_select/ci_select_grid_ctrl.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');


class ci_Select_WP extends TCMS_Admin_Page_W_Grid_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_ci/');

		return $ret;
	}


	protected function init_transit_vars() {
		$this->add_transit_var('vpage');
	}


	protected function create_grid_ctrl() {
		$tvm = $this->get_tvm();

		$ret = new CI_Select_Grid_Ctrl('ci_select', $tvm, Tangra_Parameter_Method::GET, $this->_config->get_hidden_dir().'inc/');

		$context = $this->get__context();
		if ($context->exists_in_get('selectfor')) {
			$ret->set_additional_parameters(array('ci_type' => $context->get_from_get('selectfor')));
		}
//		} elseif ($context->exists_in_get('linkto')) {
//			$ret->set_additional_parameters(array('ci_type' => $context->get_from_get('linkto')));
//		}


		if (!$tvm->is_page_var_registered('ci', 'text')) {
			$tvm->register_page_var('ci', 'text');
			$tmp1 = null;
			$tvm->register_page_var('ci', 'id');
			$tvm->set_page_var('ci', 'id', $tmp1);
		}

		$this->process_filter($tvm);

		return $ret;
	}


	protected function default_event($page_num = NULL) {
		$vpage_id = $this->get_transit_var('vpage');

		$dbc = $this->get_dbc();
		$vpage = new TCMS_VPage_DBC();
		if (!$vpage->load_by_id($dbc, $vpage_id)) {
			throw new Tangra_Module_Exception('Cannot find vpage with id = '.$vpage_id);
		}
		$this->export('vpage', $vpage);

		return parent::default_event($page_num);
	}


	private function process_filter($vm) {
		$context = $this->get__context();
		if ($context->exists_in_get('text')) {
			$text = $context->get_from_get('text');
			$vm->set_page_var('ci', 'text', $text);
		}

		if ($context->exists_in_get('id')) {
			$id = $context->get_from_get('id');
			if ($id) {
				if (!(tangra_is_int($id) && $id > 0)) {
					$id = '';
				}
				$text = '';
				$vm->set_page_var('ci', 'text', $text);
			}
			$vm->set_page_var('ci', 'id', $id);
		}

		$text = $vm->get_page_var('ci', 'text');
		$id = $vm->get_page_var('ci', 'id');

		$this->export('text', $text);
		$this->export('id', $id);
	 }
}